//
//  GameAction.m
//  rolling
//
//  Created by Mac on 10-7-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "GameAction.h"
#import "Ball.h"
#import "Person.h"
#import "SysInfo.h"

@implementation GameAction

@synthesize person;
@synthesize sysInfo;

- (id) init{
	self.person = [[Person alloc] init];
	self.sysInfo = [[SysInfo alloc] init];
	return self;
}

- (void) action{
	int i;
	[person isAtBoundary];
	for (i=0; i<sysInfo.ballNum; i++) {
		Ball *ball = (Ball *)[sysInfo.balls objectAtIndex:i];
		//Step 1 : refresh location
		ball.location = CGPointMake(ball.location.x + ball.speedX, ball.location.y + ball.speedY);
		//Step 2 : is out of screan?
		if ([ball isOutOfScreen]) {
			[ball dealloc];
			ball = [[Ball alloc] init];
			[sysInfo.balls replaceObjectAtIndex:i withObject:ball];
		}
		//Step 3 : is crashed?
		int revise = (ball.width - self.person.width) / 2;	//revise the difference of geometry location and screen location.
		int distance = (self.person.location.x - ball.location.x + revise) * (self.person.location.x - ball.location.x + revise) 
							+ (self.person.location.y - ball.location.y + revise) * (self.person.location.y - ball.location.y + revise);
		int safeDistance = (self.person.width + ball.width) * (self.person.width + ball.width);
		if (distance <= safeDistance) {
			//sleep(2);
			//TODO: 
			UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Crash!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
			[baseAlert show];
			[baseAlert release];
			
		}

		//Step 4 : 
		
	}

}
@end
